package com.hzsk.lisence.controller;

import com.hzsk.lisence.entity.SoftwareEntity;
import com.hzsk.lisence.entity.query.SelectByNameQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.mapper.SoftwareMapper;
import com.hzsk.lisence.service.SoftwareService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Base64;
import java.util.Objects;
import javax.annotation.Resource;



@CrossOrigin
@RestController
@RequestMapping("/api/lisence/softwareinfo/")
public class SoftwareController {
    @Resource
    public SoftwareService softwareService;

    @Resource
    private SoftwareMapper softwareMapper;



    @PostMapping("getsoftwarelistselected")
    @ApiOperation(value = "搜索软件列表")
    public IncResponce getSoftwareListSelected(@RequestBody SelectByNameQuery selectByNameQuery) {
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareService.getSoftwareListSelected(selectByNameQuery));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @GetMapping("getallsoftware")
    @ApiOperation(value = "根据操作人id获取软件名称")
    public IncResponce getAllSoftware(@RequestParam(required = false,value = "operatorUserId ") Integer operatorUserId) {
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareService.getAllSoftware(operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("savesoftwareinfo")
    @ApiOperation(value="创建软件")
    public IncResponce saveSoftwareInf(SoftwareEntity softwareEntity) {
        IncResponce incResponce = new IncResponce();
        try {
           synchronized (this){
               MultipartFile file = softwareEntity.getFile();
               String type = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
               InputStream inputStream = file.getInputStream();
               byte[] bytes = IOUtils.toByteArray(inputStream);
               String log ="data:image/"+ type + ";base64," +Base64.getEncoder().encodeToString(bytes);
               softwareEntity.setSoftwareLogo(log);
               softwareEntity.setFile(null);//将文件制空，否则日志AOP切面报错
               incResponce.setCode(0);
               String name=softwareEntity.getSoftwareName();
               Integer account = softwareMapper.isAccount(name);
               if (account > 0){
                   incResponce.setCode(-1);
                   incResponce.setErrorMessage("软件已存在");
               } else {
                   softwareService.saveSoftwareInf(softwareEntity,softwareEntity.getOperatorUserId(),incResponce);
               }
           }
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PutMapping("updatesoftwareinfo")
    @ApiOperation(value = "修改软件信息")
    public IncResponce updateUserInfo(SoftwareEntity softwareEntity){
        IncResponce incResponce = new IncResponce();
        try {
            MultipartFile file = softwareEntity.getFile();
            if (file != null) {
                String type = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
                InputStream inputStream = file.getInputStream();
                byte[] bytes = IOUtils.toByteArray(inputStream);
                String log ="data:image/"+ type + ";base64," +Base64.getEncoder().encodeToString(bytes);
                softwareEntity.setSoftwareLogo(log);
                softwareEntity.setFile(null);//将文件制空
            }
            softwareService.updateSoftwareInf(softwareEntity,softwareEntity.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @DeleteMapping("delsoftwareinfo")
    @ApiOperation(value = "删除软件信息")
    public IncResponce delUserinfo(@RequestParam("softwareId")Integer softwareId,@RequestParam("userId")Integer userId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareService.deleteSoftwareInf(softwareId,userId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }
}
